package com.buptse.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.buptse.listener.ExcelUploadListener;
import com.buptse.model.ExcelPojo;
import com.buptse.model.Information;
import com.buptse.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/data")
@CrossOrigin
public class DataController {
    @Autowired
    private InformationService informationService;

    @RequestMapping("/upload")
    @ResponseBody
    @CrossOrigin
    public List<Information> dataUpload(MultipartFile file, HttpServletRequest request) throws IOException, ParseException {
        String fileName=file.getOriginalFilename();
        String category;
        int index=fileName.indexOf(".");
        String suffix=fileName.substring(index+1);
        //文件后缀匹配
        if(suffix.equals("mp3")){ //音频
            category="audio";
        }else if(suffix.equals("mp4")){ //视频
            category="video";
        }else if(suffix.equals("xls")||suffix.equals("xlsx")||suffix.equals("csv")){ //excel
            category="excel";
            ExcelTypeEnum excelTypeEnum;
            if(suffix.equals("xls")){
                excelTypeEnum=ExcelTypeEnum.XLS;
            }else if(suffix.equals("xlsx")){
                excelTypeEnum=ExcelTypeEnum.XLSX;
            }else{
                excelTypeEnum=ExcelTypeEnum.CSV;
            }
            EasyExcel.read(file.getInputStream(), ExcelPojo.class,new ExcelUploadListener(informationService)).excelType(excelTypeEnum).sheet().doRead();
        }else{ //图片
            category="photo";
        }
        String filePath=request.getServletContext().getRealPath(category);
        File newFile=new File(filePath);
        if(!newFile.exists()){
            newFile.mkdir();
        }
        if(!suffix.equals("xls") && !suffix.equals("xlsx") && !suffix.equals("csv")){
            informationService.photoAndVideoDecode(fileName.substring(0,index),newFile+File.separator+System.currentTimeMillis()+fileName);
        }
        file.transferTo(new File(newFile+File.separator+System.currentTimeMillis()+fileName));
        return informationService.getEarthquakeInformation();
    }

    //数据可视化
    @RequestMapping("/show")
    @ResponseBody
    @CrossOrigin
    public List<Information> dataShow(){
        return informationService.getEarthquakeInformation();
    }

    //根据月份可视化
    @RequestMapping("/dataVisualization")
    @ResponseBody
    @CrossOrigin
    public List<Map<String,Object>> dataShowMonthly(){
        return informationService.getMonthlyCount();
    }

    //省份可视化
    @RequestMapping("/dataVisualProvince")
    @ResponseBody
    @CrossOrigin
    public List<Map<String,Object>> dataShowProvince(){
        return informationService.getProvinceEarthquakeCount();
    }

    //删除数据
    @RequestMapping("/deleteData")
    @ResponseBody
    @CrossOrigin
    public List<Information> deleteData(@RequestBody Map<String,Integer> map){
        return informationService.deleteData(map.get("id"));
    }

    //根据文本插入数据
    @RequestMapping("/insertData")
    @ResponseBody
    public void insertData(@RequestBody Map<String,String> param) throws ParseException {
        Information information=new Information();
        String location=param.get("location");
        information.setLocation(location);
        String dataCarrier="文字";
        information.setCarrier(dataCarrier);
        String category=param.get("category");
        information.setDirectory(category);
        String disasterIndicator=param.get("disasterIndicator");
        information.setIndexing(disasterIndicator);
        String code="无";
        information.setCode(code);
        String description=param.get("description");
        information.setDescription(description);
        String subSource=param.get("subSource");
        information.setSource(subSource);
        String dateString=param.get("date")+" 00:00:00";
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=simpleDateFormat.parse(dateString);
        information.setDating(date);
        informationService.insertData(information);
    }
}
